/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int a;
        ListNode *p1 = l1, *p2 = l2;
        ListNode* p1p;
        do{
            p1->val += (p2 ? p2->val : 0);
            if(p1->val > 9) {
                if(p1->next == NULL) {
                    ListNode *n = new ListNode(1, NULL);
                    p1->next = n;
                } else {
                    p1->next->val++;
                }
                p1->val -= 10;
            }
            p1p = p1;
            p1 = p1->next;
            p2 = p2 ? p2->next : NULL;
        } while(p1);
        if(p2 != NULL) p1p->next = p2;
        return l1;
    }
};